// pages/my/index.js
import {
    user,
    wechat,
    member,
    position
} from "../../api/apiData"
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showNav: false,
        showImg: false,
        myinfo: '',
        status: 1,
        token: '',
        banner:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let type = wx.getStorageSync('type')
        console.log(type)
        this.setData({
            type
        })
        position.content.detail({
			position_id: 4
		}, res => {
			if (res.code == 1) {
				this.setData({
					banner: res.data.path
				})
			}
		})
    },
    toLogin() {
        wx.navigateTo({
            url: '/pages/login/index',
        })
    },
    toList() {
        wx.navigateTo({
            url: '/pages/apply/list',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let type = wx.getStorageSync('type')
        console.log(type)
        this.setData({
            type
        })
        if (this.data.type == "Member") {
            this.GetMyInfo()
        }
        if (this.data.type == "User") {
            this.getInfo()
        }
        if (this.data.type == "" || this.data.type == undefined) {
            wx.showModal({
                title: '提示',
                content: '您未登录,请登录~',
                showCancel: true, //是否显示取消按钮
                cancelText: "否", //默认是“取消”
                // cancelColor:'skyblue',//取消文字的颜色
                confirmText: "是", //默认是“确定”
                // confirmColor: 'skyblue',//确定文字的颜色
                success: function (res) {
                    if (res.cancel) {
                        //点击取消,默认隐藏弹框
                        wx.navigateBack({
                            delta: 1,
                        })
                    } else {
                        //点击确定
                        wx.navigateTo({
                            url: "/pages/login/index",
                        })
                    }
                },
                fail: function (res) {}, //接口调用失败的回调函数
                complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
            })
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    getInfo() {
        let _this = this
        console.log(666)
        user.detail({}, res => {
            let {
                code,
                msg,
                data
            } = res
            if (code == 1) {
                _this.setData({
                    myinfo: data
                })
            } else {
              wx.showToast({
                title: res.msg,
                icon:'error'
              })
            }
        })
    },
    GetMyInfo() {
        let _this = this
        member.station.memberDetail({}, res => {
            console.log(res)
            let {
                code,
                msg,
                data
            } = res
            if (code == 1) {
                _this.setData({
                    myinfo: data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    ExitLoding() {
        // wx.setStorage({
        //     key: "token",
        //     data: ""
        // })
        // wx.setStorage({
        //     key: "type",
        //     data: ""
        // })
        wx.navigateTo({
            url: '/pages/login/index',
        })
    },
    setshrink() {
        this.setData({
            is_shrink: !this.data.is_shrink
        })
    },
    toBind() {
        wx.showLoading({
            title: "请稍等...",
          })
        let is_bind_wxapp = this.data.myinfo.is_bind_wxapp
        let myinfo = this.data.myinfo
        let _this = this
        let iv, encryptedData
        if (is_bind_wxapp == 0) {
            wx.getUserProfile({
                desc: "个人信息",
                success: function (res) {
                    iv = res.iv
                    encryptedData = res.encryptedData
                    wx.login({
                        success(res2) {
                            let {
                                code
                            } = res2
                            member.wxapp.bindUser({
                                code,
                                iv,
                                encryptedData
                            }, res => {
                                if (res.code == 1) {
                                    wx.showToast({
                                        title: res.msg,
                                    })
                                    myinfo.is_bind_wxapp = 1
                                    _this.setData({
                                        myinfo
                                    })
                                } else {
                                    wx.showToast({
                                        title: res.msg,
                                        icon: 'error'
                                    })
                                }
                            })
                        }
                    })
                },
                fail(err) {


                }
            })
        } else {
            member.wxapp.unbindUser({}, res => {
                if (res.code == 1) {
                    wx.showToast({
                        title: res.msg,
                    })
                    myinfo.is_bind_wxapp = 0
                    _this.setData({
                        myinfo
                    })
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }

            })
        }


    },


})